package com.engine4cloud.tdf.supervision.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.engine4cloud.tdf.common.core.exception.ValidateCodeException;
import com.engine4cloud.tdf.common.data.web.query.Pages;
import com.engine4cloud.tdf.common.data.web.query.Queries;
import com.engine4cloud.tdf.common.data.web.response.PageInfo;
import com.engine4cloud.tdf.common.data.web.response.Params;
import com.engine4cloud.tdf.supervision.convert.RcglDetachedConvert;
import com.engine4cloud.tdf.supervision.dto.create.RcglDetachedCreateDTO;
import com.engine4cloud.tdf.supervision.dto.query.RcglDetachedQueryDTO;
import com.engine4cloud.tdf.supervision.dto.result.JzdxResultDTO;
import com.engine4cloud.tdf.supervision.dto.result.RcglDetachedResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.RcglDetachedUpdateDTO;
import com.engine4cloud.tdf.supervision.entity.RcglDetachedEntity;
import com.engine4cloud.tdf.supervision.mapper.RcglDetachedMapper;
import com.engine4cloud.tdf.supervision.service.JzdxService;
import com.engine4cloud.tdf.supervision.service.RcglDetachedService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.Optional;

/**
 * 脱管
 *
 * @author wenjie
 * @date 2024-05-30 12:01:53
 */
@Service
public class RcglDetachedServiceImpl extends ServiceImpl<RcglDetachedMapper, RcglDetachedEntity> implements RcglDetachedService {
    @Resource
    private JzdxService jzdxService;

    @Override
    public PageInfo<RcglDetachedResultDTO> getPageList(Params pageable, RcglDetachedQueryDTO dto) {
        Page<RcglDetachedEntity> page = page(Pages.page(pageable), wrapper(dto));
        return Pages.convert(page, RcglDetachedConvert.INSTANCE::convert);
    }

    @Override
    public RcglDetachedResultDTO getById(Long id) {
        return RcglDetachedConvert.INSTANCE.convert(super.getById(id));
    }

    @Override
    public Boolean create(RcglDetachedCreateDTO dto) {
        JzdxResultDTO dx = Optional.ofNullable(jzdxService.getByCode(dto.getRybh()))
                .orElseThrow(() -> new ValidateCodeException("矫正对象数据不存在"));

        return super.save(RcglDetachedConvert.INSTANCE.create(dto, dx.getTenantId()));
    }

    @Override
    public Boolean update(RcglDetachedUpdateDTO dto) {
        Optional.ofNullable(super.getById(dto.getId()))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));

        return super.updateById(RcglDetachedConvert.INSTANCE.update(dto));
    }

    private LambdaQueryWrapper<RcglDetachedEntity> wrapper(RcglDetachedQueryDTO query) {
        LambdaQueryWrapper<RcglDetachedEntity> wrapper = Queries.lambda();
        Queries.accept(query.getId(), v -> wrapper.eq(RcglDetachedEntity::getId, v));
        Queries.accept(query.getRybh(), v -> wrapper.eq(RcglDetachedEntity::getRybh, v));
        if (Objects.nonNull(query.getStartTime()) && Objects.nonNull(query.getEndTime())) {
            wrapper.between(RcglDetachedEntity::getFxtgsj, query.getStartTime(), query.getEndTime());
        }
        Queries.accept(query.getCzjg(), v -> wrapper.like(RcglDetachedEntity::getCzjg, v));
        return wrapper;
    }
}